#import "SFUWebSource.h"

/**
 * An SFUWebSource is an object representing an SFU website 
 * with parsable events.
 *
 * Conforms to the WebParsable protocol.
 *
 * @author      James Lin
 * @version     1.0.1
 * @since       0.0.2
 * @see         WebParsable
 */
@implementation SFUWebSource

@synthesize sourceURL;
@synthesize sourceCode;

/**
 * Initialize the SFU website load request with a URL.
 *
 * The connection is asynchronous.
 *
 * @param sourceURLString  the absolute URL of the SFU website
 * @param theDelegate      the delegate to handle connection events
 * @return                 the initialized SFUWebSource
 * @see	                   WebParsable#initWithURLString
 * @see	                   SFUConnectionDelegate
 */
- (id) initWithURLString:(NSString*)sourceURLString
				delegate:(id<SFUConnectionDelegate>)theDelegate {
	if (self = [super init]) {
		self.sourceURL = [NSURL URLWithString:sourceURLString];
		delegate = theDelegate;
	}
	
	return self;
}

/**
 * Get the delegate handling connection events.
 *
 * @return  the delegate handling connection events
 */
- (id<SFUConnectionDelegate>)delegate {
	return delegate;
}

/**
 * Initialize the load request to SFU website and retrieve source code.
 *
 * @return  whether it was successful
 */
- (BOOL)downloadSourceCode {
	NSError *error;
	self.sourceCode = [NSString stringWithContentsOfURL:sourceURL 
											   encoding:NSUTF8StringEncoding 
												  error:&error];
	return ([self.sourceCode length] > 0);
}

/**
 * Parse elements from the website.
 *
 * @return  an array of the parsed elements
 * @see	    WebParsable#parseElements
 */
- (NSArray*) parseElements {
	return nil;
}

/**
 * Deallocate the SFUWebSource.
 */
- (void)dealloc {
	[sourceURL release];
	sourceURL = nil;
	[sourceCode release];
	sourceCode = nil;
	delegate = nil;
	[super dealloc];
}

@end
